﻿using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Coder.Workflow.Mapping
{
    public class ProcessInstanceMapping : IEntityTypeConfiguration<ProcessInstance>

    {
        private readonly Type _numberGenerator;

        public ProcessInstanceMapping(Type numberGenerator)
        {
            _numberGenerator = numberGenerator;
        }

        public void Configure(EntityTypeBuilder<ProcessInstance> builder)
        {
            builder.HasKey(_ => _.Id);
            builder.Property(_ => _.Id).ValueGeneratedOnAdd();

            builder.Property(_ => _.Number).HasMaxLength(20).HasValueGenerator(_numberGenerator);


            builder.Property(_ => _.Status);
            builder.Property(_ => _.FinishTime);
            builder.Property(_ => _.CreateTime).HasDefaultValueSql("'0001-00-00 00:00:00'")
                .ValueGeneratedOnAdd();

            builder.HasOne(_ => _.WorkProcess);
            builder.HasOne(_ => _.User);

            builder.HasIndex(_ => _.Number);
        }
    }
}